#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : i forgot
# @Author  : zym
# @FileName: draw.py
# @Software: PyCharm
# @Description: 利用处理好的数据绘制图像
from typing import List
import pyecharts.options as opts
from pyecharts.globals import ThemeType
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Timeline, Grid, Bar, Map, Pie, Line

"""
Gallery 使用 pyecharts 1.3.0
From pyecharts 交流分享群 -- 郭昱
"""
data = [
    {
        "time": "2014年",
        "data": [{'name': '广东', 'value': [0.5951596905611978, 8.412687779022372, '广东']},
                 {'name': '江苏', 'value': [0.7025939331774697, 9.931289852113375, '江苏']},
                 {'name': '山东', 'value': [0.38322967893901644, 5.417019478467891, '山东']},
                 {'name': '辽宁', 'value': [0.27184538118948964, 3.8425826755169674, '辽宁']},
                 {'name': '浙江', 'value': [0.4245989000410919, 6.001780755672229, '浙江']},
                 {'name': '河北', 'value': [0.23346204680898955, 3.3000274366759736, '河北']},
                 {'name': '河南', 'value': [0.254539234732172, 3.597957226057908, '河南']},
                 {'name': '上海', 'value': [0.38517291710617074, 5.444487494598374, '上海']},
                 {'name': '四川', 'value': [0.22261103204313895, 3.1466464185082526, '四川']},
                 {'name': '湖北', 'value': [0.23632668500311116, 3.340519605600055, '湖北']},
                 {'name': '湖南', 'value': [0.19204436417549003, 2.714581147130193, '湖南']},
                 {'name': '黑龙江', 'value': [0.2088356638032659, 2.9519291453444096, '黑龙江']},
                 {'name': '福建', 'value': [0.23293797076082512, 3.292619528789077, '福建']},
                 {'name': '安徽', 'value': [0.19677451619321867, 2.781442684804126, '安徽']},
                 {'name': '北京', 'value': [0.45787109585343966, 6.4720891443802095, '北京']},
                 {'name': '广西', 'value': [0.14329157935721396, 2.0254518873053535, '广西']},
                 {'name': '云南', 'value': [0.11044553624170292, 1.5611672425463208, '云南']},
                 {'name': '江西', 'value': [0.1347714819573391, 1.9050187994992542, '江西']},
                 {'name': '吉林', 'value': [0.2101297635237579, 2.9702214744051254, '吉林']},
                 {'name': '山西', 'value': [0.13694343277302934, 1.9357197094795082, '山西']},
                 {'name': '陕西', 'value': [0.21276239646413195, 3.0074342079208187, '陕西']},
                 {'name': '重庆', 'value': [0.14179444916506076, 2.0042896865197855, '重庆']},
                 {'name': '天津', 'value': [0.24165401156274133, 3.4158223113340442, '天津']},
                 {'name': '内蒙古', 'value': [0.16464259675965312, 2.3272523049408695, '内蒙古']},
                 {'name': '新疆', 'value': [0.12544405835037997, 1.7731740125748598, '新疆']},
                 {'name': '贵州', 'value': [0.09507138146123637, 1.343850838082719, '贵州']},
                 {'name': '甘肃', 'value': [0.09448443904572751, 1.3355543029439139, '甘肃']},
                 {'name': '海南', 'value': [0.07877610853844204, 1.113514265314893, '海南']},
                 {'name': '青海', 'value': [0.0674998620274637, 0.9541225210139632, '青海']},
                 {'name': '宁夏', 'value': [0.08220273834257913, 1.1619502853170471, '宁夏']},
                 {'name': '西藏', 'value': [0.03663171428378797, 0.5177957781201151, '西藏']}]
    }
]

time_list = [str(d) + "年" for d in range(2014, 2015)]

total_num = [
    3.4,
    4.5,
    5.8,
    6.8,
    7.6,
    8.3,
    8.8,
    9.9,
    10.9,
    12.1,
    14,
    16.8,
    19.9,
    23.3,
    28,
    33.3,
    36.5,
    43.7,
    52.1,
    57.7,
    63.4,
    68.4,
    72.3,
    78,
    84.7,
    91.5,
]
maxNum = 1
minNum = 0


def get_year_chart(year: str):
    map_data = [
        [[x["name"], x["value"]] for x in d["data"]] for d in data if d["time"] == year
    ][0]
    min_data, max_data = (minNum, maxNum)
    data_mark: List = []
    i = 0
    for x in time_list:
        if x == year:
            data_mark.append(total_num[i])
        else:
            data_mark.append("")
        i = i + 1

    map_chart = (
        Map()
            .add(
            series_name="",
            data_pair=map_data,
            zoom=1,
            center=[119.5, 34.5],
            is_map_symbol_show=False,
            itemstyle_opts={
                "normal": {"areaColor": "#323c48", "borderColor": "#404a59"},
                "emphasis": {
                    "label": {"show": Timeline},
                    "areaColor": "rgba(255,255,255, 0.5)",
                },
            },
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="" + str(year) + "全国区域创新能力分布 数据来源：中国区域创新能力监测报告2015",
                subtitle="",
                pos_left="center",
                pos_top="top",
                title_textstyle_opts=opts.TextStyleOpts(
                    font_size=25, color="rgba(255,255,255, 0.9)"
                ),
            ),
            tooltip_opts=opts.TooltipOpts(
                is_show=True,
                formatter=JsCode(
                    """function(params) {
                    if ('value' in params.data) {
                        return params.data.value[2] + ': ' + params.data.value[0];
                    }
                }"""
                ),
            ),
            visualmap_opts=opts.VisualMapOpts(
                is_calculable=True,
                dimension=0,
                pos_left="30",
                pos_top="center",
                range_text=["High", "Low"],
                range_color=["lightskyblue", "yellow", "orangered"],
                textstyle_opts=opts.TextStyleOpts(color="#ddd"),
                min_=min_data,
                max_=max_data,
            ),
        )
    )

    line_chart = (
        Line()
            .add_xaxis(time_list)
            .add_yaxis("", total_num)
            .add_yaxis(
            "",
            data_mark,
            markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]),
        )
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="此图表未完成 可绘制全国创新能力随年份增长的折线图", pos_left="72%", pos_top="5%"
            )
        )
    )
    bar_x_data = [x[0] for x in map_data]
    bar_y_data = [{"name": x[0], "value": x[1][0]} for x in map_data]
    bar = (
        Bar()
            .add_xaxis(xaxis_data=bar_x_data)
            .add_yaxis(
            series_name="",
            y_axis=bar_y_data,
            label_opts=opts.LabelOpts(
                is_show=True, position="right", formatter="{b} : {c}"
            ),
        )
            .reversal_axis()
            .set_global_opts(
            xaxis_opts=opts.AxisOpts(
                max_=maxNum, axislabel_opts=opts.LabelOpts(is_show=False)
            ),
            yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
            tooltip_opts=opts.TooltipOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(
                is_calculable=True,
                dimension=0,
                pos_left="10",
                pos_top="top",
                range_text=["High", "Low"],
                range_color=["lightskyblue", "yellow", "orangered"],
                textstyle_opts=opts.TextStyleOpts(color="#ddd"),
                min_=min_data,
                max_=max_data,
            ),
        )
    )

    pie_data = [[x[0], x[1][0]] for x in map_data]
    pie = (
        Pie()
            .add(
            series_name="",
            data_pair=pie_data,
            radius=["15%", "35%"],
            center=["80%", "82%"],
            itemstyle_opts=opts.ItemStyleOpts(
                border_width=1, border_color="rgba(0,0,0,0.3)"
            ),
        )
            .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{b} {d}%"),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )

    grid_chart = (
        Grid()
            .add(
            bar,
            grid_opts=opts.GridOpts(
                pos_left="10", pos_right="45%", pos_top="50%", pos_bottom="5"
            ),
        )
            .add(
            line_chart,
            grid_opts=opts.GridOpts(
                pos_left="65%", pos_right="80", pos_top="10%", pos_bottom="50%"
            ),
        )
            .add(pie, grid_opts=opts.GridOpts(pos_left="45%", pos_top="60%"))
            .add(map_chart, grid_opts=opts.GridOpts())
    )

    return grid_chart


if __name__ == "__main__":
    timeline = Timeline(
        init_opts=opts.InitOpts(width="1600px", height="900px", theme=ThemeType.DARK)
    )
    for y in time_list:
        g = get_year_chart(year=y)
        timeline.add(g, time_point=str(y))

    timeline.add_schema(
        orient="vertical",
        is_auto_play=True,
        is_inverse=True,
        play_interval=5000,
        pos_left="null",
        pos_right="5",
        pos_top="20",
        pos_bottom="20",
        width="60",
        label_opts=opts.LabelOpts(is_show=True, color="#fff"),
    )

    timeline.render("Distribution_of_regional_innovation_capability_in_China.html")
